package com.usabb.service.impl;

import com.usabb.domain.*;
import com.usabb.pojo.vo.CombosAddVO;
import com.usabb.pojo.vo.CombosNewAddVO;
import com.usabb.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.usabb.dao.MaternityCombosDao;
import com.ifast.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <pre>
 * 月子中心 套餐表
 * </pre>
 * <small> 2019-08-19 18:03:27 | Aron</small>
 */
@Slf4j
@Service
public class MaternityCombosServiceImpl extends CoreServiceImpl<MaternityCombosDao, MaternityCombosDO>
        implements MaternityCombosService {

    @Autowired
    private MaternityBaseService maternityBaseService;
    @Autowired
    private CombosCategoryService combosCategoryService;
    @Autowired
    private CombosCategoryItemService combosCategoryItemService;
    @Autowired
    private CombosItemValueService combosItemValueService;


    @Deprecated
    @Transactional
    @Override
    public void addCombos(CombosAddVO combosAddVO) {
        log.debug("Vo: {}", combosAddVO);

        MaternityCombosDO combosDO = CombosAddVO.transform(combosAddVO);
        boolean insert = this.insert(combosDO);

        List<CombosCategoryDO> categoryDOS = combosDO.getCategoryDOS();
        categoryDOS.forEach(it -> it.setCombosId(combosDO.getId()));
        combosCategoryService.insertBatch(categoryDOS);

        categoryDOS.forEach(it -> {
            List<CombosCategoryItemDO> items = it.getItems();
            items.forEach(itt -> itt.setCategoryId(it.getId()));

            combosCategoryItemService.insertBatch(items);
            }
        );

        //套餐数
        final Long baseId = combosAddVO.getBaseId();
        MaternityBaseDO baseDO = maternityBaseService.selectById(baseId);
        baseDO.plusCombosCount();
        maternityBaseService.updateById(baseDO);

    }

    @Override
    @Transactional
    public void addCombos(CombosNewAddVO addVO) {
        log.debug("add combos. Vo: {}", addVO);

        final MaternityCombosDO combosDO = MaternityCombosDO.of(addVO);
        final boolean insert = this.insert(combosDO);
        log.info("insert result: {}", insert);

        final Long combosDOId = combosDO.getId();
        final List<CombosItemValueDO> valueDOS = addVO.toValueDOList(combosDOId);

        final boolean insertBatch = combosItemValueService.insertBatch(valueDOS);

        log.info("insert result: {}", insertBatch);
    }

    @Override
    public MaternityCombosDO getByIdWith(Long id) {
        MaternityCombosDO maternityCombosDO = this.selectById(id);
        final Long baseId = maternityCombosDO.getBaseId();
        final Long combosDOId = maternityCombosDO.getId();

        //get: category list with items
        List<CombosCategoryDO> combosCategoryDOS = combosCategoryService.getByBizIdWithItems(baseId);
        maternityCombosDO.setCategoryDOS(combosCategoryDOS);

        //get: combos item value
        List<CombosItemValueDO> combosItemValueDOList = combosItemValueService.getByCombosId(combosDOId);

        //replace: with item value
        final Map<Long, String> itemIdMap = combosItemValueDOList.stream()
                .collect(Collectors.toMap(CombosItemValueDO::getItemId, CombosItemValueDO::getItemIntro));

        combosCategoryDOS.forEach(category -> {
            //Long categoryId = category.getId();
            category.getItems().forEach(item -> {
                item.setItemIntro(itemIdMap.get(item.getId()));
            });
        });

        return maternityCombosDO;
    }

    @Override
    @Transactional
    public boolean updateValue(CombosNewAddVO addVO) {
        //update
        final Long combosId = addVO.getCombosId();
        MaternityCombosDO combosDO = this.selectById(combosId);
        combosDO.setCombosName(addVO.getCombosName());
        combosDO.setCombosFee(combosDO.getCombosFee());
        final boolean updateById = this.updateById(combosDO);

        //delete values
        final boolean deleteValues = combosItemValueService.deleteByCombosId(combosId);


        final List<CombosItemValueDO> valueDOS = addVO.toValueDOList(combosId);
        final boolean insertBatch = combosItemValueService.insertBatch(valueDOS);

        log.info("update result: {}, delete result: {}, insertBatch result: {}", updateById, deleteValues, insertBatch);
        return insertBatch;
    }

}
